

//_______________________________________________________________________________________________________________
/****************************************************************************************************************/
/*                                                                                                              */
/*                     D Y N A M I C   T I M E R S                                                              */
/*                                                                                                              */
/****************************************************************************************************************/

#ifdef OS_ENABLE_PTIMERS

/************************************************************************/
/* Check dynamic timer's states                                         */
/************************************************************************/

#define OS_CheckPTimer(ptimer)      ((ptimer).Flags.bTimeout)       // overflowed
#define OS_IsPTimerActive(ptimer)   ((ptimer).Flags.bActive)        // created
#define OS_IsPTimerStopped(ptimer)  (!((ptimer).Flags.bRun))        // stopped
#define OS_IsPTimerRun(ptimer)      ((ptimer).Flags.bRun)           // run

//------------------------------------------------------------------------------
// Get remaining time of counting
#define OS_GetPTimer(ptimer)      (-(ptimer).Time)


/************************************************************************/
/* Stop, pause and continue counting                                    */
/************************************************************************/
#define OS_StopPTimer(ptimer)          (ptimer).Flags.bRun = 0        // Stop timer (pause it)
#define OS_PausePTimer(ptimer)         (ptimer).Flags.bRun = 0        // Stop timer (pause it)
#define OS_ContinuePTimer(ptimer)      (ptimer).Flags.bRun = 1        // Continue after pause

#define OS_BreakPTimer(ptimer)         (ptimer).Flags.bTimeout = 1    // 

/************************************************************************/
/* Services for run dynamic timers                                      */
/************************************************************************/

//------------------------------------------------------------------------------
// Start counting with given time. Timeout flag is cleared.

#define OS_RunPTimer(ptimer, time)          \
    {                                       \
        (ptimer).Flags.bRun = 0;            \
        (ptimer).Timer = -time;             \
        (ptimer).Flags.bTimeout = 0;        \
        (ptimer).Flags.bRun = 1;            \
    }

//------------------------------------------------------------------------------
// Re-run static timer for time time since last overflow. Timeout flag is cleared.

#define OS_UpdatePTimer(ptimer, time)       \
    {                                       \
        (ptimer).Flags.bRun = 0;            \
        (ptimer).Timer -= time;             \
        (ptimer).Flags.bTimeout = 0;        \
        (ptimer).Flags.bRun = 1;            \
    }

//------------------------------------------------------------------------------
// Increase time of dinamic timer counter. Timeout flag remain unchanged.

#define OS_AddPTimer(ptimer, time)          \
    {                                       \
        (ptimer).Flags.bRun = 0;            \
        (ptimer).Timer -= time;             \
        (ptimer).Flags.bRun = 1;            \
    }


/************************************************************************/
/* Wait services                                                        */
/************************************************************************/

//------------------------------------------------------------------------------
// Wait for dynamic timer overflowed

#define OS_WaitPTimer(ptimer)                   \
    {                                           \
        _OS_WAIT_EVENT(OS_CheckPTimer(ptimer)); \
    }


//------------------------------------------------------------------------------
// Delay current task using dynamic timer

#define OS_DelayPTimer(ptimer, delay)       \
    {                                       \
        OS_RunPTimer(ptimer, delay);        \
        OS_WaitPTimer(ptimer);              \
    }



#endif  // OS_ENABLE_PTIMERS

